/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.firstlife.mps.unittests;

import j2meunit.framework.Test;
import j2meunit.framework.TestCase;
import j2meunit.framework.TestMethod;
import j2meunit.framework.TestSuite;

import org.bouncycastle.util.encoders.Hex;

import com.aroundj2me.location.LocationController;
import com.aroundj2me.model.Comment;
import com.aroundj2me.model.Constants;
import com.aroundj2me.model.Encrypter;
import com.aroundj2me.model.Location;
import com.aroundj2me.model.Place;
import com.aroundj2me.xml.XMLEncoder;

public class XMLEncoderTest extends TestCase {
	private XMLEncoder xmlEncoder;
	private final String username = "kito";
	private final String password = "mypass";
	
	public XMLEncoderTest(String string, TestMethod method) {
		super(string, method);
	}

	public XMLEncoderTest() {
	}

	protected void setUp() throws Exception {
		xmlEncoder = new XMLEncoder();
	}

	protected void tearDown() throws Exception {
		xmlEncoder = null;
	}
	
	public void testEncodePlaceToXML(){
		
		Place p = new Place();
		p.setId(123);
		p.setName("Pizzeria Chicken");
		p.setAddress("Via Resia ...");
		p.setCity("Bolzano");
		p.setPhone("0471 932198");
		p.setUser("kito");
		
		String encodedXML = xmlEncoder.encodePlaceToXML(p, username, password);
		
		assertNotNull(encodedXML);
		
		StringBuffer xmlString = new StringBuffer();
		xmlString.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
		xmlString.append("<root>");
		xmlString.append("<type>");
		xmlString.append(Constants.COMMIT_PLACE);
		xmlString.append("</type>");
		xmlString.append("<validationData>");
		xmlString.append("<user>" + username + "</user>");
		xmlString.append("<password>" + new String(Hex.encode(new Encrypter().encryptPassword(password))) + "</password>");
		xmlString.append("</validationData>");
		xmlString.append("<requestBody>");
		xmlString.append("<place>");
		xmlString.append("<id>" + p.getId() + "</id>");
		xmlString.append("<name>" + p.getName()  + "</name>");
		xmlString.append("<address>" + p.getAddress()  + "</address>");
		xmlString.append("<city>" + p.getCity()  + "</city>");
		xmlString.append("<phone>" + p.getPhone()  + "</phone>");
		xmlString.append("<user>" + p.getUser() + "</user>");
		if(p.getLocation() != null){
			xmlString.append("<location>");
			xmlString.append("<cellid>" + p.getLocation().getCellid() + "</cellid>");
			xmlString.append("<lat>" + p.getLocation().getLat() + "</lat>");
			xmlString.append("<lon>" + p.getLocation().getLon() + "</lon>");
			xmlString.append("</location>");
		}
		xmlString.append("</place>");
		xmlString.append("</requestBody>");
		xmlString.append("</root>");
		assertEquals("XML String", xmlString.toString(), encodedXML);
	}
	
	public void testEncodeCommentToXML(){
		
		Comment c = new Comment();
		c.setPlaceId(1234);
		c.setContent("this is the comment content");
		c.setUser("kito");
		
		String encodedStr = xmlEncoder.encodeCommentToXML(c, username, password);
		
		StringBuffer correctXmlString = new StringBuffer();
		correctXmlString.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
		correctXmlString.append("<root>");
		correctXmlString.append("<type>");
		correctXmlString.append(Constants.COMMIT_PLACE_COMMENT);
		correctXmlString.append("</type>");
		correctXmlString.append("<validationData>");
		correctXmlString.append("<user>" + username + "</user>");
		correctXmlString.append("<password>" + new String(Hex.encode(new Encrypter().encryptPassword(password))) + "</password>");
		correctXmlString.append("</validationData>");
		correctXmlString.append("<requestBody>");
		correctXmlString.append("<comment>");
		correctXmlString.append("<placeid>" + c.getPlaceId() + "</placeid>");
		correctXmlString.append("<content>" + c.getContent() + "</content>");
		correctXmlString.append("</comment>");
		correctXmlString.append("</requestBody>");
		correctXmlString.append("</root>");
		
		assertEquals("XML String", correctXmlString.toString(), encodedStr);
	}
	
	public void testEncodeRequestParamALL(){
		String encodedStr = xmlEncoder.encodeRequest(Constants.PARAM_RETRIEVE_GLOBAL,0,999999, username, password);
		
		StringBuffer correctXmlString = new StringBuffer();
		correctXmlString.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
		correctXmlString.append("<root>");
		correctXmlString.append("<type>");
		correctXmlString.append(Constants.RETRIEVE_PLACE);
		correctXmlString.append("</type>");
		correctXmlString.append("<validationData>");
		correctXmlString.append("<user>" + username + "</user>");
		correctXmlString.append("<password>" + new String(Hex.encode(new Encrypter().encryptPassword(password))) + "</password>");
		correctXmlString.append("</validationData>");
		correctXmlString.append("<requestBody>");
		correctXmlString.append("<requestParam>");
		correctXmlString.append(Constants.PARAM_RETRIEVE_GLOBAL);
		correctXmlString.append("</requestParam>");
		correctXmlString.append("<beginData>0</beginData>");
		correctXmlString.append("<dataAmount>999999</dataAmount>");
		correctXmlString.append("</requestBody>");
		correctXmlString.append("</root>");
		
		assertNotNull(encodedStr);
		assertEquals("Strings should be equal", correctXmlString.toString(), encodedStr);
	}
	
	public void testEncodeRequestLocation(){
		Location randomLocation = new LocationController(null).getRandomLocation();
		String encodedStr = xmlEncoder.encodeRequest(Constants.PARAM_LOCATION_CELLID,0,9999999, null, randomLocation, username, password);
		
		StringBuffer correctXmlString = new StringBuffer();
		correctXmlString.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
		correctXmlString.append("<root>");
		correctXmlString.append("<type>");
		correctXmlString.append(Constants.RETRIEVE_PLACE);
		correctXmlString.append("</type>");
		correctXmlString.append("<validationData>");
		correctXmlString.append("<user>" + username + "</user>");
		correctXmlString.append("<password>" + new String(Hex.encode(new Encrypter().encryptPassword(password))) + "</password>");
		correctXmlString.append("</validationData>");
		correctXmlString.append("<requestBody>");
		correctXmlString.append("<requestParam>");
		correctXmlString.append(Constants.PARAM_LOCATION_CELLID);
		correctXmlString.append("</requestParam>");
		correctXmlString.append("<beginData>0</beginData>");
		correctXmlString.append("<dataAmount>9999999</dataAmount>");
		correctXmlString.append("<location>");
		correctXmlString.append("<cellid>" + randomLocation.getCellid() + "</cellid>");
		correctXmlString.append("<lat>" + randomLocation.getLat() + "</lat>");
		correctXmlString.append("<lon>" + randomLocation.getLon() + "</lon>");
		correctXmlString.append("</location>");
		correctXmlString.append("</requestBody>");
		correctXmlString.append("</root>");
		
		assertNotNull(encodedStr);
		assertEquals("Strings should be equal", correctXmlString.toString(), encodedStr);
	}
	
	public void testEncodeSearchRequest(){
		String searchQuery = "sell computer";
		
		String encodedStr = xmlEncoder.encodeSearchRequest(Constants.PARAM_SEARCH,0,9999999, searchQuery, new Location("3", "10,4", "13,40"), true, username, password);
		
		StringBuffer correctXmlString = new StringBuffer();
		correctXmlString.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
		correctXmlString.append("<root>");
		correctXmlString.append("<type>");
		correctXmlString.append(Constants.RETRIEVE_PLACE);
		correctXmlString.append("</type>");
		correctXmlString.append("<validationData>");
		correctXmlString.append("<user>" + username + "</user>");
		correctXmlString.append("<password>" + new String(Hex.encode(new Encrypter().encryptPassword(password))) + "</password>");
		correctXmlString.append("</validationData>");
		correctXmlString.append("<requestBody>");
		correctXmlString.append("<requestParam>");
		correctXmlString.append(Constants.PARAM_SEARCH);
		correctXmlString.append("</requestParam>");
		correctXmlString.append("<cellid>3</cellid>");
		correctXmlString.append("<beginData>0</beginData>");
		correctXmlString.append("<dataAmount>9999999</dataAmount>");
		correctXmlString.append("<searchQuery>" + searchQuery + "</searchQuery>");
		correctXmlString.append("</requestBody>");
		correctXmlString.append("</root>");
		
		assertEquals("Strings should be equal", correctXmlString.toString(), encodedStr);
	}
	
	public void testEncodeDeleteRequest(){
		
		Place p = new Place();
		p.setId(123);
		p.setName("Pizzeria Chicken");
		p.setAddress("Via Resia ...");
		p.setCity("Bolzano");
		p.setPhone("0471 932198");
		p.setUser("kito");
		
		String encodedStr = xmlEncoder.encodeDeleteRequest(p, username, password);
		
		StringBuffer correctXmlString = new StringBuffer();
		correctXmlString.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
		correctXmlString.append("<root>");
		correctXmlString.append("<type>");
		correctXmlString.append(Constants.DELETE_PLACE);
		correctXmlString.append("</type>");
		correctXmlString.append("<validationData>");
		correctXmlString.append("<user>" + username + "</user>");
		correctXmlString.append("<password>" + new String(Hex.encode(new Encrypter().encryptPassword(password))) + "</password>");
		correctXmlString.append("</validationData>");
		correctXmlString.append("<requestBody>");
		correctXmlString.append("<place>");
		correctXmlString.append("<id>" + p.getId() + "</id>");
		correctXmlString.append("<name>" + p.getName() + "</name>");
		correctXmlString.append("<address>" + p.getAddress() + "</address>");
		correctXmlString.append("<city>" + p.getCity() + "</city>");
		correctXmlString.append("<phone>" + p.getPhone() + "</phone>");
		correctXmlString.append("<user>" + p.getUser() + "</user>");
		if(p.getLocation() != null){
			correctXmlString.append("<location>");
			correctXmlString.append("<cellid>" + p.getLocation().getCellid() + "</cellid>");
			correctXmlString.append("<lat>" + p.getLocation().getLat() + "</lat>");
			correctXmlString.append("<lon>" + p.getLocation().getLon() + "</lon>");
			correctXmlString.append("</location>");
		}
		correctXmlString.append("</place>");
		correctXmlString.append("</requestBody>");
		correctXmlString.append("</root>");
		
		assertEquals("Strings should be equal", correctXmlString.toString(), encodedStr);
	}

	public Test suite() {
		TestSuite suite = new TestSuite();
		suite.addTest(new XMLEncoderTest("XMLEncoderTest:testEncodePlaceToXML", new TestMethod() {
			public void run(TestCase tc) {
				((XMLEncoderTest) tc).testEncodePlaceToXML();
			}
		}));
		suite.addTest(new XMLEncoderTest("XMLEncoderTest:testEncodeCommentToXML", new TestMethod() {
			public void run(TestCase tc) {
				((XMLEncoderTest) tc).testEncodeCommentToXML();
			}
		}));
		suite.addTest(new XMLEncoderTest("XMLEncoderTest:testEncodeRequestParamALL", new TestMethod(){
			public void run(TestCase tc) {
				((XMLEncoderTest) tc).testEncodeRequestParamALL();
			}
		}));
		suite.addTest(new XMLEncoderTest("XMLEncoderTest:testEncodeSearchRequest", new TestMethod(){
			public void run(TestCase tc) {
				((XMLEncoderTest) tc).testEncodeSearchRequest();
			}
		}));
		suite.addTest(new XMLEncoderTest("XMLEncoderTest:testEncodeDeleteRequest", new TestMethod(){
			public void run(TestCase tc) {
				((XMLEncoderTest) tc).testEncodeDeleteRequest();
			}
		}));
		suite.addTest(new XMLEncoderTest("XMLEncoderTest:testEncodeRequestLocation", new TestMethod(){
			public void run(TestCase tc) {
				((XMLEncoderTest) tc).testEncodeRequestLocation();
			}
		}));
		return suite;
	}
}
